package MediaOnline.Adapter;


import java.util.ArrayList;
import java.util.concurrent.ExecutionException;

import MediaOnline.Data.videoData;
import MediaOnline.GUI.R;
import MediaOnline.General.DownloadImages;
import MediaOnline.WebService.getVideoByPage;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.ViewDebug.FlagToString;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class VideoAdapter extends BaseAdapter {
	public Context context;
	//arrListFilm de gan vao gridview
	public static ArrayList<String> arrListFilm = new ArrayList<String>();
    
    public VideoAdapter(Context ctx) {
        context=ctx;
    }

    public int getCount() {
        return arrListFilm.size();
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public void insertValue (){
    	
    }
    
    //Get list video su dung webservice va gan vao arrListFilm
    public void getListVideo(final Context x, int FilmCatID){
		getVideoByPage g = new getVideoByPage(x);
		g.filmCatID=FilmCatID;		
		g.execute();
    }
    
    //Lay danh sach downloaded video
    public void getDownloadedVideo(){
    	//arrListFilm.clear();
    	arrListFilm=videoData.lstFilmCat;
    }
    
    public View getView(final int position, View convertView, ViewGroup parent) {
    	
    	
    	
    	View v = convertView;
        if (v == null) {
        	Log.d("Context::::", context.toString());
        	LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        	convertView = inflater.inflate(R.layout.filmsingle3, null);
        }
        
        //Hiển thị các thông tin cần thiết
        //Thumbail
        ImageView imageView = (ImageView)convertView.findViewById(R.id.FilmSingle3_filmThumb);
        imageView.setScaleType(ScaleType.FIT_CENTER);
        imageView.setPadding(5, 5, 5, 5);
        //Tên phim chính
        TextView txtTenPhimChinh = (TextView)convertView.findViewById(R.id.FilmSingle3_txtTenPhimChinh);
        txtTenPhimChinh.setText(videoData.lstVideo.get(position));
        //Tên phim phụ
        TextView txtTenPhimPhu = (TextView) convertView.findViewById(R.id.FilmSingle3_txtTenPhimPhu);
        txtTenPhimPhu.setText(videoData.lstVideoTenPhimPhu.get(position));
        //Số tập
        String TenPhimPhu = videoData.lstVideoTenPhimPhu.get(position);
        if (videoData.lstSoTap.get(position)>1) {
        	TenPhimPhu+=" ("+videoData.lstSoTap.get(position)+" tập)";
		}
        txtTenPhimPhu.setText(TenPhimPhu);
        //Lay hinh anh
        //Nếu đã có hình ảnh thì load lên image view, không thì download về
        if (videoData.lstThumb.get(position)!=null){
        	imageView.setImageDrawable(videoData.lstThumb.get(position));
		}
		else{
			DownloadImages d = new DownloadImages(context, videoData.lstThumbUrl.get(position), imageView);
        	//d.execute();
        	try {
				videoData.lstThumb.set(position, d.execute().get());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        //Cắt tên
        String FilmName = videoData.lstVideo.get(position);
        if(FilmName.length()>40){
        	FilmName = FilmName.substring(0, 37) +"...";
        }
        txtTenPhimChinh.setText(FilmName);
        return convertView;

    }
}